import http from '@/utils/http';

/**
 * 获取库存列表
 * @param {Object} params - 查询参数
 */
export function getInventoryList(params) {
  // 确保查询参数包含扁平化字段请求
  const queryParams = {
    flatten: true,  // 请求扁平化数据结构
    ...params
  };
  
  console.log('发送库存列表查询:', queryParams);
  return http.get('/inventory', { params: queryParams });
}

/**
 * 查询库存列表（与getInventoryList相同，为了兼容性保留）
 * @param {Object} params - 查询参数
 */
export function queryInventory(params) {
  return http.get('/inventory', { params });
}

/**
 * 获取库存详情
 * @param {Number|String} id - 库存ID
 */
export function getInventoryDetail(id) {
  return http.get(`/inventory/${id}`);
}

/**
 * 调整库存
 * @param {Number|String} id - 库存ID
 * @param {Object} data - 调整数据，包含以下字段:
 *   - cargo_id: 货物ID (必填)
 *   - location_id: 库位ID (必填)
 *   - adjust_quantity: 调整数量，正数增加，负数减少 (必填)
 *   - remark: 调整原因/备注 (必填)
 * @returns {Promise} - 返回API请求Promise
 */
export function adjustInventory(id, data) {
  // 打印调试信息
  console.log(`调整库存API调用 - ID: ${id}`, data);
  
  // 确保字段名称符合后端要求
  const apiData = {
    cargo_id: data.cargo_id,
    location_id: data.location_id,
    adjust_quantity: data.adjust_quantity,
    remark: data.remark,
    // 添加库存ID
    inventory_id: id
  };
  
  // 修改为正确的API路径 - 后端路由是 /inventory/adjust
  return http.post('/inventory/adjust', apiData);
}

/**
 * 获取库存预警
 * @param {Object} params - 查询参数
 */
export function getInventoryAlerts(params) {
  return http.get('/inventory/alerts', { params });
}

/**
 * 获取商品库存位置信息
 * @param {Number|String} productId - 商品ID
 * @param {Object} params - 查询参数
 */
export function getProductLocations(productId, params) {
  return http.get(`/inventory/product/${productId}/locations`, { params });
}

/**
 * 获取可用的库位列表
 * @param {Number|String} warehouseId - 仓库ID
 * @param {Object} params - 查询参数，可包含商品ID等过滤条件
 */
export function getAvailableLocations(warehouseId, params) {
  return http.get(`/inventory/warehouse/${warehouseId}/available-locations`, { params });
}

/**
 * 获取库位库存信息
 * @param {Number|String} locationId - 库位ID
 * @param {Object} params - 查询参数
 */
export function getLocationInventory(locationId, params = {}) {
  // 确保分页参数存在
  const queryParams = {
    page: 1,
    pageSize: 100,
    ...params
  };
  
  // 构建正确的API路径
  const apiPath = `/inventory/locations/${locationId}/inventory`;
  
  console.log(`调用获取库位库存API: ${apiPath}，参数:`, queryParams);
  
  // 直接调用API
  return http.get(apiPath, { params: queryParams })
    .then(response => {
      console.log(`库位${locationId}库存信息获取成功:`, response);
      return response;
    })
    .catch(error => {
      console.error(`库位${locationId}库存信息获取失败:`, error);
      throw error;
    });
}

/**
 * 获取库存盘点单列表
 * @param {Object} params - 查询参数
 */
export function getInventoryCountList(params) {
  return http.get('/inventory/count', { params });
}

/**
 * 创建库存盘点单
 * @param {Object} data - 盘点单数据
 */
export function createInventoryCount(data) {
  return http.post('/inventory/count', data);
}

/**
 * 获取库存盘点单详情
 * @param {Number|String} id - 盘点单ID
 */
export function getInventoryCountDetail(id) {
  return http.get(`/inventory/count/${id}`);
}

/**
 * 完成库存盘点
 * @param {Number|String} id - 盘点单ID
 * @param {Object} data - 盘点结果数据
 */
export function completeInventoryCount(id, data) {
  return http.post(`/inventory/count/${id}/complete`, data);
}

/**
 * 获取库存概览数据
 */
export function getInventoryOverview() {
  return http.get('/inventory/overview')
}

/**
 * 获取指定仓库中有库存的商品列表
 * @param {Number} warehouseId - 仓库ID
 * @param {Object} params - 额外的查询参数
 */
export function getWarehouseProducts(warehouseId, params = {}) {
  return http.get(`/inventory/warehouse/${warehouseId}/products`, { params });
}

// 导出所有库存相关API
export default {
  getInventoryOverview,
  getInventoryAlerts,
  getWarehouseProducts,
  getProductLocations,
  getAvailableLocations,
  queryInventory,
  getInventoryList,
  getInventoryDetail,
  adjustInventory,
  getInventoryCountList,
  createInventoryCount,
  getInventoryCountDetail,
  completeInventoryCount,
  getLocationInventory
} 